#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Complex.h"

void sayHello() {
	printf("Hello World\n");
}

int rev(int oldVal, int maxVal) {
	int newVal = 0;
	int i;
	int oldBit = maxVal >> 1;
	int newBit = 1;

	while (newBit < maxVal) {

		if (oldVal >= oldBit) {
			newVal += newBit;
			oldVal -= oldBit;
		}
	
		oldBit >>= 1;
		newBit <<= 1;
	}

	return newVal;
}

void test_rev() {
	printf("\n\nTesting rev()...");	
	
	assert(rev(0,8)==0);	
	assert(rev(1,8)==4);	
	assert(rev(2,8)==2);	
	assert(rev(3,8)==6);	
	assert(rev(4,8)==1);	
	assert(rev(5,8)==5);	
	assert(rev(6,8)==3);	
	assert(rev(7,8)==7);	
	
	assert(rev(1,16)==8);	
	assert(rev(12,16)==3);

	printf("Done\n");

	Complex c;
	printf("My Complex=");
	c.printValue();
	printf("\n");
	
}

void test_Complex() {
	printf("\n\nTesting rev()...");	
	
	Complex c (1.0,1.0);
	Complex z;

	assert(c == c + z);
	assert( !(c == c + c));
	
	printf("Done\n");
}

void printArray(Complex *arr, int n) { 
	for (int i=0; i < n; i++) {
		arr[i].printValue();
		printf("\n");
	}
}

void revCopy(Complex *arr, int n) {

	for (int i=0; i < n; i++) {
		if (i < rev(i,n)) {
			printf("swapping %d with %d\n",i,rev(i,n));	 
			arr[i].swap(&(arr[rev(i,n)])); 
		}
	}	
}


void test_revCopy() {
	#define N 8

	Complex vals[N];
	for (int i=0; i < N; i++) { vals[i] = Complex(i*1.0,0.0); }

	printf("\nBefore\n");
	printArray(vals,N);

	printf("\nAfter\n");
	revCopy(vals,N);
	printArray(vals,N);
}


